package demolinkedlist;

public class MyLinkedList {

    static class ListNode {//内部类

        //定义节点上的内容
        public int val;
        public ListNode prev;//前驱
        public ListNode next;//后继

        public ListNode(int val) {//构造方法
            this.val = val;
        }
    }

    //链表的头节点、尾节点
    public ListNode head;
    public ListNode last;


    /**
     * 判断该链表是否包含节点值key
     */
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    /**
     * 求链表长度
     * @return
     */
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    /**
     * 打印链表
     */
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 头插法
     * @param data
     */
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
            //return;
        } else {
            node.next = head;
            head.prev = node;
            head = node;//head指向node
        }
    }

    /**
     * 尾插法
     * @param data
     */
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = last = node;
        } else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    /**
     * 在链表的任意位置插入新节点
     * @param index
     * @param data
     */
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {//插入位置不合法
            return;
        }
        if (index == 0) {//使用头插法
            addFirst(data);
            return;
        }
        if (index == size()) {//使用尾插法
            addLast(data);
            return;
        }
        ListNode cur = searchIndex(index);
        ListNode node = new ListNode(data);
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
        node.next = cur;
    }
    //让cur走到index位置
    private ListNode searchIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    /**
     * 删除节点key
     * @param key
     */
    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {//如果cur==head
                    head = head.next;
                    if (head != null) {//判断链表是不是只有一个节点
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else {
                    cur.prev.next = cur.next;//cur的前驱的后继 = cur的后继
                    if (cur.next == null) {//如果cur==last
                        last = cur.prev;//last往前移
                    } else {//cur既不是head也不是last
                        cur.next.prev = cur.prev;//cur的后继的前驱 = cur的前驱
                    }
                }
                return;
            } else {
                cur = cur.next;
            }
        }
    }

    /**
     * 删除所有值为key的节点
     * @param key
     */
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {//如果cur==head
                    head = head.next;
                    if (head != null) {//判断链表是不是只有一个节点
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else {
                    cur.prev.next = cur.next;//cur的前驱的后继 = cur的后继
                    if (cur.next == null) {//如果cur==last
                        last = cur.prev;//last往前移
                    } else {//cur既不是head也不是last
                        cur.next.prev = cur.prev;//cur的后继的前驱 = cur的前驱
                    }
                }
            }
            cur = cur.next;
        }
    }

    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curN;
        }
        head = null;
        last = null;
    }
}
